package business;

import java.util.ArrayList;

public class XML {

    //
//    public static String get_panoID() {
//        return "p2Xzq_K8zSqlfjExQBHrww";
//    }

    public static String get_panoID(String xml) {
        String pano_id = "pano_id=";
        String fim = " num_zoom";

        int indexInicial = xml.indexOf(pano_id) + pano_id.length() + 1;
        int indexFinal = xml.indexOf(fim) - 1;

        return xml.substring(indexInicial, indexFinal);
    }

    public static ArrayList<String> getVizinhos(String xml) {
        ArrayList<String> vizinhos = new ArrayList<String>();

        String annotation_properties = getAnnotatioProprieties(xml);

        //Link

        //Inicio
        String inicioLink = "<link";
        int indexInicialLink = annotation_properties.indexOf(inicioLink);

        //Fim
        String finalLink = "</link>";
        int indexFinalLink = annotation_properties.indexOf(finalLink) + finalLink.length();

        int finalAnnotation = annotation_properties.indexOf("</annotation_properties>");

        String inicioPanoId = "pano_id=";
        String fimPanoId = " road_argb";

        String vizinho;
        String link;

        while ((indexInicialLink) <= finalAnnotation) {
            link = annotation_properties.substring(indexInicialLink, indexFinalLink);

            vizinho = link.substring(link.indexOf(inicioPanoId) + inicioPanoId.length() + 1, link.indexOf(fimPanoId) - 1);

            vizinhos.add(vizinho);

            indexInicialLink = indexFinalLink + 1;
            indexFinalLink = annotation_properties.indexOf(finalLink, indexInicialLink) + finalLink.length();
        }

        //System.out.println("Vizinho 1 = " + vizinhos.get(0));
        //System.out.println("Vizinho 2 = " + vizinhos.get(1));

        return vizinhos;

    }

    public static String getAnnotatioProprieties(String xml_archive) {

        //Inicio
        String inicioAnnotationProperties = "<annotation_properties>";
        int indexInicialAnnotations = xml_archive.indexOf(inicioAnnotationProperties);

        //Fim
        String finalAnnotationProperties = "</annotation_properties>";
        int indexFinalAnnotations = xml_archive.indexOf(finalAnnotationProperties) + finalAnnotationProperties.length();

        //annotation_properties
        String annotation_properties = xml_archive.substring(indexInicialAnnotations, indexFinalAnnotations);

        //System.out.println(annotation_properties);

        return annotation_properties;
    }

    static double getNorte(String myXML) {
        String yaw = "pano_yaw_deg=";
        String fimYaw = " tilt_yaw_deg";
        
        String yawValue = getElement(myXML, yaw, fimYaw);

       return Double.parseDouble(yawValue);

    }

    static double getLatitude(String myXML) {
        String condicaoLatitude = "lat=";
        String condicaoFimLatitude = " lng";
        
        String latitude = getElement(myXML, condicaoLatitude, condicaoFimLatitude);
        
        return Double.parseDouble(latitude);
    }

    static double getLongitude(String myXML) {
        String condicaoLongitude = "lng=";
        String condicaoFimLongitude = " original_lat=";
        
        String longitude = getElement(myXML, condicaoLongitude, condicaoFimLongitude);
        
        return Double.parseDouble(longitude);

    }
    
    private static String getElement(String myXML, String condicaoInicial, String condicaoFinal) {
        int indexCondicaoInicial = myXML.indexOf(condicaoInicial) + condicaoInicial.length() + 1;
        int indexCondicaoFinal = myXML.indexOf(condicaoFinal) - 1;

        return myXML.substring(indexCondicaoInicial, indexCondicaoFinal);
    }
}
